<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://unpkg.com/vue"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .container {
      position: absolute;
      width: 200px;
      height: 400px;
      background-color: pink;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
  </style>
</head>

<body>
  <div id="root"></div>
  <script>
    /* 
      1. setup中使用provide及inject
      - 子组件如果希望改变父组件传过来的数据
      - 尽管子组件可以修改，但最好是由父组件自己修改
      2. ref获取DOM元素或组件实例
    */

    const app = Vue.createApp({
      template: `
      <div class="container" >
        <todolist />
      </div>
    `,
    })
    app.component('todolist', {
      template: `
      <div class="container" >
        <child />
      </div>
      `,
      setup(props, context) {
        const { provide } = Vue
        provide('name', 'xiaomi')
        return {}
      }
    })
    app.component('child', {
      template: '<h3 ref="h3Ref">子组件{{name}}</h3>',
      setup() {
        const { inject, ref, onMounted } = Vue
        const name = inject('name', '')
        const h3Ref = ref(null)
        onMounted(() => {
          console.log(h3Ref)
        })
        return { name, h3Ref }
      }
    })
    const vm = app.mount('#root');

  </script>
</body>

</html>